package com.defect.controller.basics;

import com.defect.bo.ResultBoHu;
import com.defect.po.Project;
import com.defect.service.basicsService.ProjectManagementService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 项目管理控制层
 * @author 胡八一
 * @date 2020/07/27 11:59
 */
@RestController
@RequestMapping("/hubayi")
public class ProjectManagementController {
    //项目信息的service层对象
    @Resource
    private ProjectManagementService projectManagementService;

    /**
     * 分页查询所有项目信息
     * @param project
     * @return 为ResultBoHu对象
     */
    @PostMapping("/queryAllProjectByPage")
    @CrossOrigin(origins = "http://127.0.0.1:8020")
    /*@RequestBody加了这个注解后 可以直接接收ajax传递的json格式的对象数据 会帮你提取转换成参数类型的对象*/
    public ResultBoHu queryAllProjectByPage(@RequestBody Project project){
        System.out.println("分页查询所有项目信息进来了");
        PageInfo<Project> pageInfo = projectManagementService.queryAllProjectByPage(project);
        return ResultBoHu.getResultBo().setCode("200").setObject(pageInfo).setMassge("查询成功");
    }

    /**
     * 新增修改项目信息对象
     * @param project
     * @return 为ResultBoHu对象
     */
    @PostMapping("/saveProjects")
    @CrossOrigin(origins = "http://127.0.0.1:8020")
    public ResultBoHu saveProjects(@RequestBody Project project){
        System.out.println("新增或修改信息进来了:id:"+project.getProjectId()+"名字:"+project.getProjectName()+"简介:"+project.getProjectIntro());
        if(project.getProjectId() == null || project.getProjectId() == ""){
            try{
                //表示为新增
                projectManagementService.insertProject(project);
                return ResultBoHu.getResultBo().setCode("200").setMassge("新增成功");
            }catch (Exception e){
                return ResultBoHu.getResultBo().setCode("404").setMassge("新增失败").setObject(e);
            }
        }else{
            try{
                //表示为修改
                System.out.println("修改来了");
                projectManagementService.updateProject(project);
                return ResultBoHu.getResultBo().setCode("200").setMassge("修改成功");
            }catch (Exception e){
                return ResultBoHu.getResultBo().setCode("404").setMassge("修改失败").setObject(e);
            }
        }
    }


}
